<?php

namespace App\Http\Service;

use App\Models\Commentreport;
use App\Models\Doubanpost;
use App\Models\Baoliaopost;
use App\Http\Service\UserService;
use App\Http\Service\BaseService;
use App\Http\Service\CommentService;
use Illuminate\Support\Facades\DB;
class CommentreportService  extends BaseService
{
    private $model;

    public function __construct(Commentreport $model,Doubanpost  $doubanpost,UserService $userService,Baoliaopost $baoliaopost,CommentService $commentService)
    {
        parent::__construct();
        $this->model = $model;
        $this->doubanpost=$doubanpost;
        $this->userService=$userService;
        $this->baoliaopost=$baoliaopost;
        $this->commentService=$commentService;
       
    }
    //评论区举报-列表
    public function commentreportlist($request,$data=[])
    {
        $page = intval($request->json('page')) ?$request->json('page') :$this->cpage;
        $limit = intval($request->json('limit')) ?$request->json('limit') :$this->climit;
        $offset = ($page-1)*$limit;
        $where=[];
        if($data['keyword']){
            $keyword=$data['keyword'];
            $where[]=[
                'name', 'like', "%$keyword%"
            ];

        }

        $count = $this->model
            ->from('comment_report as A') // 主表使用别名
            ->join('comment as B', 'A.comment_id', '=', 'B.id')
            ->where($where)->count();
        $fields = ['A.id','A.comment_id','A.user_id','A.report_type','A.report_reason','A.report_evidence','A.report_status','A.create_time','A.update_time','B.content'];
        $list = $this->model->select($fields)
            ->from('comment_report as A') // 主表使用别名
            ->join('comment as B', 'A.comment_id', '=', 'B.id')
            ->where($where)
            ->offset($offset)->limit($limit)
            ->orderBy('A.create_time', 'desc')
            ->orderBy('A.id', 'desc')
            ->get()->toArray();
        foreach ($list as $k => &$value) {
            $user_id=$value['user_id'];
            $user=$this->userService->update_user($user_id);
            $value['nickname']=$user['nickname'];
            $value['avatar']=$user['avatar'];
        }
        $result = [
            'page'=> $page,
            'limit'=> $limit,
            'total_page'=> ceil($count/$limit),
            'count'=>$count,
            'data'=> $list
        ];
        return $result; 
        



    }
    //评论区举报-修改
    public function commentreportedit($request,$data=[])
    {
        $where=[
            'id'=>$data['id']
        ];
        $data['create_time'] =date("Y-m-d H:i:s");
        $res=$this->model->where($where)->update($data);
    }
   
    //评论区举报-添加
    public function commentreportadd($request,$data=[])
    {
        # 默认值
        $data['report_status']=1;
        $where=[
            'comment_id'=>$data['comment_id'],
            'user_id'=>$data['user_id'],
        ];
       // dd($where);
        $first=$this->model->where($where)->first();
        if($first){
            $data['update_time']=date("Y-m-d h:i:s");
            $this->model->where($where)->update($data);
        }else{
            $data['create_time']=date("Y-m-d h:i:s");
            $data['update_time']=date("Y-m-d h:i:s");
            $this->model->where($where)->insertGetId($data);
        }

       
              
    }
}
